package com.example.springbootmyelasticjob.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
import com.example.springbootmyelasticjob.dao.TestDao;
import com.example.springbootmyelasticjob.model.Student;
import com.example.springbootmyelasticjob.tool.SpringUtils;
import lombok.extern.slf4j.Slf4j;


import java.util.List;

/**
 * Dataflow类型用于处理数据流，需实现DataflowJob接口。
 * 该接口提供2个方法可供覆盖，分别用于抓取(fetchData)和处理(processData)数据。
 */
@Slf4j
public class SpringDataflowJob implements DataflowJob<Student> {

    private TestDao testDao = SpringUtils.getBean(TestDao.class);
    
    @Override
    public List<Student> fetchData(final ShardingContext shardingContext) {
        log.info(">>>>>>>>>>>>>>> dataflowjob : {} : {}", shardingContext.getJobParameter(), shardingContext.getShardingParameter());
        return testDao.findAll(Long.parseLong(shardingContext.getShardingParameter()));
    }
    
    @Override
    public void processData(final ShardingContext shardingContext, final List<Student> data) {
        for (Student each : data) {
            testDao.update(Long.valueOf(shardingContext.getShardingItem()), each.getId());
        }
    }
}
